package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static com.heima.model.common.enums.AppHttpCodeEnum.DONT_DELETE;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    @Autowired
    private WmNewsService wmNewsService;

    /**
     * 查询所有频道
     * @return
     */
    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }

    /**
     * 获取频道信息列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult getChannelList(ChannelDto dto) {
        LambdaQueryWrapper<WmChannel> lbw = new LambdaQueryWrapper<>();
        if(null != dto.getName()){
            lbw.like(WmChannel::getName,dto.getName());
        }

        lbw.orderByDesc(WmChannel::getCreatedTime);
        Page<WmChannel> dtoPage = new Page<>(dto.getPage(),dto.getSize());
        Page<WmChannel> dtos = this.page(dtoPage, lbw);
        ResponseResult result = new ResponseResult();
        result.setCode(200);
        result.setData(dtos.getRecords());
        result.setTotal((int) dtos.getTotal());
        result.setErrorMessage("操作成功");
        result.setHost("");
        return result;
    }

    /**
     * 新增频道
     * @param channel
     * @return
     */
    @Override
    public ResponseResult saveChannel(WmChannel channel) {
        List<WmChannel> list = this.list();
        for (WmChannel wmChannel : list) {
            if (wmChannel.getName().equals(channel.getName())){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
            }
        }

        channel.setCreatedTime(new Date());
        channel.setIsDefault(true);
        this.save(channel);
        return ResponseResult.okResult(200,"新增频道成功");
    }

    /**
     * 编辑频道
     * @param channel
     * @return
     */
    @Override
    public ResponseResult updateChannel(WmChannel channel) {
        LambdaQueryWrapper<WmNews> lbw = new LambdaQueryWrapper<>();
        lbw.eq(WmNews::getChannelId,channel.getId());
        List<WmNews> list = wmNewsService.list(lbw);

        if (null != list && list.size()>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_USING);
        }

        this.updateById(channel);
        return ResponseResult.okResult(200,"编辑成功");
    }

    /**
     * 删除频道
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteChannel(Integer id) {
        WmChannel wmChannel = this.getById(id);
        if(wmChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.DONT_DELETE);
        }

        this.removeById(id);
        return ResponseResult.okResult(200,"删除成功");
    }

    /**
     * 查询频道name
     * @param id
     * @return
     */
    @Override
    public String getNameById(Integer id) {
        return getById(id).getName();
    }
}